package vip.idiy.anwei_android_iot.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import vip.idiy.anwei_android_iot.R;

/**
 * ClassName LayoutTableRow
 * describe:
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 14:52 2023/4/18
 */
public class LayoutTableRow extends LinearLayout {
    private final String TAG = "myTag " + " LayoutTableRow ";
    private LinearLayout layout_reuse_table_row_linear;
    // 数据1 数据2 数据3 数据4 数据5 数据6&图片  数据7&操作
    private TextView[] layout_row_data = new TextView[7];
    private ImageView layout_row_data_img;
    private MyImageButton layout_row_button_edit;
    private MyImageButton layout_row_button_remove;
    // 编辑和删除必须的参数
    private String par;

    private EditButtonClickListener editButtonClickListener;
    private RemoveButtonClickListener removeButtonClickListener;

    public LayoutTableRow(Context context) {
        super(context);
    }

    public LayoutTableRow(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 初始化
     *
     * @param isShow     1-7列是否显示 6、7列为覆盖布局,如果显示的话 imageShow和buttonShow必须为false
     * @param imageShow  6
     * @param buttonShow 7
     * @return
     */
    public LayoutTableRow init(boolean[] isShow, boolean imageShow, boolean buttonShow) {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_reuse_table_row, this, true);
        // 整行
        layout_reuse_table_row_linear = findViewById(R.id.layout_reuse_table_row_linear);
        // 数据
        for (int i = 0; i < layout_row_data.length; i++) {
            layout_row_data[i] = findViewById(getResources().getIdentifier("layout_row_data_" + (i + 1), "id", getContext().getPackageName()));
            // 如果不显示设置不占用空间
            layout_row_data[i].setVisibility(isShow[i] ? View.VISIBLE : View.GONE);
        }
        // 如果isShow[5] = false, 且 imageShow = false, 则隐藏该控件
        FrameLayout layout_row_data_frameLayout_img = findViewById(R.id.layout_row_data_frameLayout_img);
        layout_row_data_frameLayout_img.setVisibility(!(!isShow[5] && !imageShow) ?  View.VISIBLE : View.GONE);

        // 图片
        layout_row_data_img = findViewById(R.id.layout_row_data_img);
        layout_row_data_img.setVisibility(imageShow ? View.VISIBLE : View.GONE);

        // 操作栏
        LinearLayout layout_row_data_handle = findViewById(R.id.layout_row_data_handle);
        layout_row_data_handle.setVisibility(buttonShow ? View.VISIBLE : View.GONE);
        // 编辑
        layout_row_button_edit = findViewById(R.id.layout_row_button_edit);
        // 删除
        layout_row_button_remove = findViewById(R.id.layout_row_button_remove);
        return this;
    }

    /**
     * 设置编辑按钮点击事件
     *
     * @param listener
     */

    public LayoutTableRow setEditButtonClickListener(EditButtonClickListener listener) {
        editButtonClickListener= listener;
        layout_row_button_edit.setOnClickListener(v -> {
            if (editButtonClickListener!= null){
                editButtonClickListener.onEditButtonClicked(par);
            }
        });
        return this;
    }



    /**
     * 设置删除按钮点击事件
     *
     * @param listener
     */

    public LayoutTableRow setRemoveButtonClickListener(RemoveButtonClickListener listener) {
        removeButtonClickListener = listener;
        layout_row_button_remove.setOnClickListener(v -> {
            if (removeButtonClickListener != null){
                removeButtonClickListener.onRemoveButtonClicked(par);
            }
        });
        return this;
    }


    /**
     * 设置数据
     *
     * @param index
     * @param data
     * @return
     */

    public LayoutTableRow setData(int index, String data) {
        layout_row_data[index].setText(data);
        return this;
    }

    public LayoutTableRow setTextColor(int index,int color){
        layout_row_data[index].setTextColor(color);
        return this;
    }


    /**
     * 设置图片
     *
     * @param resId
     */
    public LayoutTableRow setImage(int resId) {
        layout_row_data_img.setImageResource(resId);
        return this;
    }

    public LayoutTableRow setBackground(int resId){
        layout_reuse_table_row_linear.setBackground(ResourcesCompat.getDrawable(getResources(),resId, null));
        return this;
    }

    /**
     * 设置编辑和删除使用的参数
     * @param par
     * @return
     */
    public LayoutTableRow setEditAndRemovePar(String par){
        this.par = par;
        return this;
    }



    public interface EditButtonClickListener {
        void onEditButtonClicked(String text);

    }
    public interface RemoveButtonClickListener {
        void onRemoveButtonClicked(String text);

    }
}

